package cn.turingbyte.FileIO;

import java.io.*;

public class Loong20200107 {
    public static void main(String[] args) {
        File file1 = new File("D:\\test\\1.txt");
        if (!file1.exists()){
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件不存在，已经创建新文件!");
        }

//        try {
//            FileWriter fileWriter = new FileWriter(file1);
//            fileWriter.write("Hello World");
//            fileWriter.flush();
//            fileWriter.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        try {
//            FileReader fileReader = new FileReader(file1);
//            char[] chars = new char[100];
//            try {
//                fileReader.read(chars);
//                for (char c:chars) {
//                    System.out.print(c);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }

        try {
            FileInputStream fileInputStream = new FileInputStream(file1);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            while(true){
                try {
                    String a = bufferedReader.readLine();
                    if (a == null){
                        break;
                    }
                    System.out.println(a);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
